package janelas;

import Classes.*;
import javax.swing.JOptionPane;
/**
 *
 * @author Daniela
 */
public class JanelaPrincipal extends javax.swing.JFrame {

    private Sistema sistema;
    public JanelaPrincipal() {
        initComponents();
        setResizable(false);
        setLocationRelativeTo(null);
        sistema = new Sistema();
    }
    
    public Sistema getSistema(){
        return sistema;
    }
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        nomeBiblioteca = new javax.swing.JLabel();
        janelaLogin = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        userName = new javax.swing.JTextField();
        senha = new javax.swing.JPasswordField();
        bEntrar = new javax.swing.JButton();
        cadastreSe = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Social-Library-DSC");
        setBackground(new java.awt.Color(204, 204, 255));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        nomeBiblioteca.setFont(new java.awt.Font("Franklin Gothic Medium Cond", 1, 24)); // NOI18N
        nomeBiblioteca.setText("Social-Library-DSC");

        janelaLogin.setBackground(new java.awt.Color(204, 204, 255));

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel2.setText("Usuário");

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel3.setText("Senha");

        userName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                userNameActionPerformed(evt);
            }
        });
        userName.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                userNameKeyPressed(evt);
            }
        });

        senha.setToolTipText("");
        senha.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
        senha.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                senhaKeyPressed(evt);
            }
        });

        bEntrar.setText("Entrar");
        bEntrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bEntrarActionPerformed(evt);
            }
        });

        cadastreSe.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        cadastreSe.setText("Cadastre-se");
        cadastreSe.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        cadastreSe.setDebugGraphicsOptions(javax.swing.DebugGraphics.NONE_OPTION);
        cadastreSe.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cadastreSeMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout janelaLoginLayout = new javax.swing.GroupLayout(janelaLogin);
        janelaLogin.setLayout(janelaLoginLayout);
        janelaLoginLayout.setHorizontalGroup(
            janelaLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(janelaLoginLayout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(janelaLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(janelaLoginLayout.createSequentialGroup()
                        .addGroup(janelaLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(janelaLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(bEntrar)
                            .addComponent(senha, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE)
                            .addComponent(userName, javax.swing.GroupLayout.Alignment.LEADING)))
                    .addComponent(cadastreSe, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(31, Short.MAX_VALUE))
        );
        janelaLoginLayout.setVerticalGroup(
            janelaLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(janelaLoginLayout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(janelaLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(userName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(22, 22, 22)
                .addGroup(janelaLoginLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(senha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addComponent(bEntrar)
                .addGap(9, 9, 9)
                .addComponent(cadastreSe, javax.swing.GroupLayout.DEFAULT_SIZE, 25, Short.MAX_VALUE)
                .addGap(20, 20, 20))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(113, 113, 113)
                        .addComponent(janelaLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(133, 133, 133)
                        .addComponent(nomeBiblioteca, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(121, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(15, 15, 15)
                .addComponent(nomeBiblioteca, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(janelaLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(55, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void userNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_userNameActionPerformed
       
    }//GEN-LAST:event_userNameActionPerformed

    private void bEntrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bEntrarActionPerformed
       String login = userName.getText();
       String senha1 = String.valueOf(senha.getPassword());
       Usuario usuarioEncontrado = sistema.procuraUsuario(login, senha1);
      
       if (usuarioEncontrado == null){
           senha.setText("");
           JOptionPane.showMessageDialog(null, "Usuário ou senha inválido!");
       }else{
           sistema.setUsuarioAtivo(usuarioEncontrado);
           new ContaDoUsuario(this).setVisible(true);
           this.setVisible(false);
       }
        
        senha.setText("");
        
    }//GEN-LAST:event_bEntrarActionPerformed

    private void cadastreSeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cadastreSeMouseClicked
        this.setEnabled(false); 
        this.setVisible(false);
        new CadastroDeUsuario(this).setVisible(true);
   
    }//GEN-LAST:event_cadastreSeMouseClicked

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
       
    }//GEN-LAST:event_formWindowClosed

    private void userNameKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_userNameKeyPressed
        if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_ENTER)
            bEntrar.doClick();
    }//GEN-LAST:event_userNameKeyPressed

    private void senhaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_senhaKeyPressed
        if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_ENTER)
            bEntrar.doClick();
    }//GEN-LAST:event_senhaKeyPressed

   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bEntrar;
    private javax.swing.JLabel cadastreSe;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel janelaLogin;
    private javax.swing.JLabel nomeBiblioteca;
    private javax.swing.JPasswordField senha;
    private javax.swing.JTextField userName;
    // End of variables declaration//GEN-END:variables
}
